const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

router.post('/deliver/getBillByNo', async ctx => {
    let tenantId = ctx.user.tenantId;
    let teants = await tool.relCompany(tenantId);
    let tids = teants.join(',');
    let billNo = ctx.request.body.billNo;
    if(!billNo) return ctx.err('请输入票号')
    let sql = `select a.id,a.tenantName,a.billType,a.qty totalQty,a.buName,a.tenantName,a.billDate,a.billNo,a.transTypeName,a.transType,a.address,isnull(c.state, 0) loadState,a.areaName, a.areaId 
        from (
                SELECT a.*,b.name buName,c.name tenantName,b.address,isnull(ar.name, '未定位') areaName,b.areaId  from invoice a inner join contact b on a.buId=b.id and a.tenantId in(${tids}) and a.billNo like '%${billNo}%' 
                                         inner join tenant c on a.tenantId=c.id left join area ar on b.areaId=ar.id
            ) a left join cargo_invoice c on a.id=c.iid   order by a.tenantId,a.buName,a.billDate  ;`
    let list = await db.query(sql, {type: QueryTypes.SELECT})
    return ctx.suc('', list)
})


module.exports = router;
